<template>
  <div class="resource-list">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <!-- form表单区域 -->
        <el-form :inline="true" :model="formInline" class="demo-form-inline" ref="formInline">
          <el-form-item label="资源名称" prop="name">
            <el-input v-model="formInline.name" placeholder="资源名称"></el-input>
          </el-form-item>
          <el-form-item label="资源路径" prop="url">
            <el-input v-model="formInline.url" placeholder="资源路径"></el-input>
          </el-form-item>
          <el-form-item label="资源分类" prop="region">
            <el-select v-model="formInline.region" placeholder="资源分类" clearable>
              <el-option v-for="item in resourceCategories" :label="item.name" :value="item.id" :key="item.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button @click="onReset" :disabled="isLoading">重置</el-button>
            <el-button type="primary" @click="onSubmit" :disabled="isLoading">查询</el-button>
          </el-form-item>
        </el-form>
      </div>
      <!-- table区域 -->
       <el-table
        :data="resource"
        style="width: 100%; margin-bottom: 20px"
        v-loading= 'isLoading'
        >
        <el-table-column
          prop="id"
          label="编号"
          width="180">
        </el-table-column>
        <el-table-column
          prop="name"
          label="资源名称"
          width="180">
        </el-table-column>
        <el-table-column
          prop="url"
          label="资源路径">
        </el-table-column>
        <el-table-column
          prop="description"
          label="描述">
        </el-table-column>
        <el-table-column
          label="添加时间">
          <template slot-scope="scope">
            <span>{{ scope.row.createdTime | dataFormat }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="操作">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">修改</el-button>
            <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页功能 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page.sync="formInline.current"
        :page-sizes="[5, 10, 20]"
        :page-size="formInline.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="totalCount">
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import { getResourcePages, getResourceCategory } from '@/services/resource'

export default {
  name: 'resourceList',
  data () {
    return {
      resource: [],
      resourceCategories: [],
      totalCount: 0,
      formInline: {
        name: '',
        url: '',
        region: '',
        current: 1,
        size: 10
      },
      isLoading: false
    }
  },
  methods: {
    onSubmit () {
      console.log('提交')
    },
    async loadResource () {
      this.isLoading = true
      const { data } = await getResourcePages({
        current: this.formInline.current,
        size: this.formInline.size
      })
      if (data.code === '000000') {
        this.resource = data.data.records
        this.totalCount = data.data.total
        this.isLoading = false
      }
    },
    handleEdit (index, row) {
      console.log(index, row)
    },
    handleDelete (index, row) {
      console.log(index, row)
    },
    handleSizeChange (val) {
      this.formInline.size = val
      this.formInline.current = 1
      this.loadResource()
    },
    handleCurrentChange (val) {
      this.formInline.current = val
      this.loadResource()
    },
    async loadResourceCategory () {
      const { data } = await getResourceCategory()
      if (data.code === '000000') {
        this.resourceCategories = data.data
      }
    },
    onReset () {
      this.$refs.formInline.resetFields()
    }
  },
  created () {
    this.loadResource()
    this.loadResourceCategory()
  },
  filters: {
    // 日期过滤器
    dataFormat (date) {
      date = new Date(date)
      return `
        ${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}
        ${date.getHours()}:${date.getMinutes()}:${date.getSeconds()}
      `
    }
  }
}
</script>

<style>

</style>
